﻿using System;
using Unity.Mathematics;
using UnityEngine;

namespace com.yoozoo.gta.Art
{
    public class OpenTheDoor : MonoBehaviour
    {
        [Header("门")] public GameObject doorObj;
        [Header("门HUD")] public GameObject doorHUD;
        [Header("开门角度")] public float doorAngle = 90.0f;
        [Header("开门速度")] public float doorSpeed = 2.0f;
        [Header("距离内显示HUD")] public float openDistance = 2.0f;

        [Header("挂载HUD的两个点位")] public GameObject[] doorHudPoint = new GameObject[2];

        private bool _isDoorOpen = false;
        private bool _isDoorMoving = false;
        private float _targetAngle = 0.0f;
        private GameObject player;

        private void Start()
        {
            player = BeautyGirlController.CurrPlayer;
            doorHUD.SetActive(true);
            if (player == null || doorObj == null)
            {
                UnityEngine.Debug.LogError("Player or doorObj is null");
            }
        }

        void Update()
        {
            ShowDoorHUD();
            HandleDoorInput();
            UpdateDoorRotation();
        }

        private void CalculateTheBearing()
        {
            // 使用相机方向和门的方向计算player在doorObj的正面还是背面
            Vector3 cameraDir = math.normalize( Camera.main.transform.position - doorObj.transform.position);
            Vector3 doorDir = -doorObj.transform.forward;
            //UnityEngine.Debug.LogError(doorDir);
            float dot = Vector3.Dot(cameraDir, doorDir);
            if (dot > 0)
            {
                // 正面
                //doorHUD.transform.parent = doorHudPoint[0].transform;
                doorHUD.transform.localPosition = doorHudPoint[0].transform.localPosition;
                doorHUD.transform.localRotation = doorHudPoint[0].transform.localRotation;
            }
            else
            {
                // 背面
                //doorHUD.transform.parent = doorHudPoint[1].transform;
                doorHUD.transform.localPosition = doorHudPoint[1].transform.localPosition;
                doorHUD.transform.localRotation = doorHudPoint[1].transform.localRotation;
            }
        }

        private void ShowDoorHUD()
        {
            if (player == null || doorObj == null)
            {
                return;
            }
            // 使用主角的位置和门的位置计算距离，如果距离够近则显示门的HUD
            float distance = Vector3.Distance(player.transform.position, doorObj.transform.position);
            if (distance < openDistance)
            {
                //UnityEngine.Debug.LogError($"进入范围,距离{distance}");
                CalculateTheBearing();
                if (doorHUD.activeSelf == false)
                {
                    doorHUD.SetActive(true);
                    //UnityEngine.Debug.LogError($"ShowDoorHUD，距离{distance},主角位置{player.transform.position},门位置{doorObj.transform.position}");
                }
            }else{
                //UnityEngine.Debug.LogError($"离开范围,距离{distance}");
                if (doorHUD.activeSelf == true)
                {
                    doorHUD.SetActive(false);
                    //UnityEngine.Debug.LogError($"DisableDoorHUD，距离{distance},主角位置{player.transform.position},门位置{doorObj.transform.position}");
                }
            }
        }

        private void HandleDoorInput()
        {
            if (Input.GetKeyDown(KeyCode.E) && doorHUD.activeSelf)
            {
                if (!_isDoorMoving)
                {
                    _isDoorOpen = !_isDoorOpen;
                    _isDoorMoving = true;
                    _targetAngle = _isDoorOpen ? doorAngle : 0.0f;
                }
            }
        }

        private void UpdateDoorRotation()
        {
            if (_isDoorMoving)
            {
                Quaternion currentRotation = doorObj.transform.localRotation;
                Quaternion targetRotation = Quaternion.Euler(0, _targetAngle, 0);
                doorObj.transform.localRotation = Quaternion.RotateTowards(currentRotation, targetRotation, doorSpeed * Time.deltaTime * 100);

                if (Quaternion.Angle(currentRotation, targetRotation) < 0.1f)
                {
                    _isDoorMoving = false;
                    doorObj.transform.localRotation = targetRotation; // 确保最终角度正确
                }
            }
        }


    }
}
